package com.eeprint.system.service.platform.impl;

import com.eeprint.common.utils.CodeUtil;
import com.eeprint.common.utils.IDUtil;
import com.eeprint.system.domain.platform.FORM.PlatformAddOrUpdateTenantFORM;
import com.eeprint.system.domain.platform.FORM.PlatformUserUpdateFORM;
import com.eeprint.system.domain.platform.PlatformTenant;
import com.eeprint.system.domain.platform.PlatformUser;
import com.eeprint.system.domain.tenant.TenantUser;
import com.eeprint.system.mapper.platform.PlatformLoginMapper;
import com.eeprint.system.mapper.platform.PlatformTenantMapper;
import com.eeprint.system.mapper.platform.PlatformUserMapper;
import com.eeprint.system.mapper.tenant.TenantUserMapper;
import com.eeprint.system.service.platform.PlatformTenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.math.BigInteger;
import java.util.Date;
import java.util.List;

/**
  * @Author newhonor
  * @Date 2020/10/18
  * @Version 1.0
**/

@Service
public class PlatformTenantUserServiceImpl implements PlatformTenantService {

    @Autowired
    private PlatformTenantMapper platformTenantMapper;

    @Autowired
    private PlatformUserMapper platformUserMapper;

    @Autowired
    private TenantUserMapper tenantUserMapper;

    @Override
    public List<PlatformTenant> findTenants(int page, int limit) {
        if(page<=0||page>countTenants(limit)) return null;
        int index = (page-1)*limit;
        return platformTenantMapper.findTenants(index, limit);
    }

    @Override
    public int countTenants(int limit) {
        int total = platformTenantMapper.countTenants();
        int pages = total/limit;
        if(total%limit>0) pages++;
        return pages;
    }

    @Override
    public boolean addTenant(PlatformAddOrUpdateTenantFORM platformAddOrUpdateTenantFORM, HttpSession session) {

        //企业分配ID
        PlatformTenant platformTenant = new PlatformTenant();
        platformTenant.setId(IDUtil.getId());

        //创建企业管理员
        TenantUser tenantUser = new TenantUser();
        tenantUser.setId(IDUtil.getId());
        tenantUser.setUserNick("admin-nick");
        tenantUser.setUserPhone(platformAddOrUpdateTenantFORM.getTenantAdminPhone());
        tenantUser.setUserLogo("user-logo");
        tenantUser.setUserPassword(platformAddOrUpdateTenantFORM.getUserPassword());
        tenantUser.setRelTenantId(platformTenant.getId());
        tenantUser.setCreateAt(new Date());
        tenantUser.setCreateUserId(BigInteger.valueOf(999));//测试
        tenantUser.setUpdateAt(new Date());
        tenantUser.setUpdateUserId(BigInteger.valueOf(999));//测试
        tenantUser.setUserRole(1);
        tenantUserMapper.addTenantUser(tenantUser);

        //创建企业
        platformTenant.setTenantName(platformAddOrUpdateTenantFORM.getTenantName());
        platformTenant.setTenantLocation(platformAddOrUpdateTenantFORM.getTenantLocation());
        platformTenant.setTenantAdminName(platformAddOrUpdateTenantFORM.getTenantAdminName());
        platformTenant.setTenantAdminPhone(platformAddOrUpdateTenantFORM.getTenantAdminPhone());
        platformTenant.setTenantTerminalCode(CodeUtil.getCode());
        //platformTenant.setRelAdminUserId((BigInteger) session.getAttribute("adminId"));
        platformTenant.setRelAdminUserId(tenantUser.getId());//测试
        platformTenant.setCreateAt(new Date());
        //platformTenant.setCreateUserId((BigInteger) session.getAttribute("adminId"));
        platformTenant.setCreateUserId(BigInteger.valueOf(9877));//测试
        platformTenant.setUpdateAt(new Date());
        //platformTenant.setUpdateUserId((BigInteger) session.getAttribute("adminId"));
        platformTenant.setUpdateUserId(BigInteger.valueOf(98788));//测试

        platformTenantMapper.addTenant(platformTenant);

        return true;


    }

    @Override
    public boolean updateTenant(PlatformAddOrUpdateTenantFORM platformAddOrUpdateTenantFORM, HttpSession session) {
        PlatformTenant platformTenant = new PlatformTenant();
        platformTenant.setTenantName(platformAddOrUpdateTenantFORM.getTenantName());
        platformTenant.setTenantLocation(platformAddOrUpdateTenantFORM.getTenantLocation());
        platformTenant.setTenantAdminName(platformAddOrUpdateTenantFORM.getTenantAdminName());
        platformTenant.setTenantAdminPhone(platformAddOrUpdateTenantFORM.getTenantAdminPhone());
        platformTenant.setUpdateAt(new Date());
        platformTenant.setUpdateUserId(BigInteger.valueOf(98788));//测试

        TenantUser tenantUser = new TenantUser();
        tenantUser.setUserPhone(platformAddOrUpdateTenantFORM.getTenantAdminPhone());
        tenantUser.setUserPassword(platformAddOrUpdateTenantFORM.getUserPassword());

        BigInteger tenantUserId = tenantUserMapper.findTenantUserIdByPhonePassword(tenantUser);
        BigInteger platformRelAdminUserId = platformUserMapper.findRelAdminUserIdByPhone(platformAddOrUpdateTenantFORM.getTenantAdminPhone());

        System.out.println(tenantUserId);
        System.out.println(platformRelAdminUserId);

        if(tenantUserId!=null&&tenantUserId.equals(platformRelAdminUserId)){
            platformTenantMapper.updateTenant(platformTenant);
            return true;
        }
        return false;
    }

}
